﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace 批量删除指定类型的文件
{
    public partial class MainFrm : Form
    {
        delegate void SetLogCallback(string imgType, string logInfo);
        private void WriteLog(string imgType, string logInfo)
        {
            ListViewItem infoMsg = new ListViewItem();
            infoMsg.ImageKey = imgType;
            infoMsg.Text = logInfo;
            infoMsg.ToolTipText = logInfo;
            infoMsg.SubItems.Add(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

            if (hisView.InvokeRequired)
            {
                SetLogCallback logCall = new SetLogCallback(WriteLog);
                this.Invoke(logCall, new object[] { imgType, logInfo });
            }
            else
            {
                hisView.Items.Add(infoMsg);
            }
        }
        public MainFrm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            if (dialog.ShowDialog(this) == DialogResult.OK)
            {
                txtOutPut.Text = dialog.SelectedPath;
                WriteLog(ImgConst.SUCC, "初始化系统配置成功！");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            #region 执行删除
            try
            {
                string dir = txtOutPut.Text;
                if (dir.Length == 0) MessageBox.Show("请选择指定路径！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                if (txtfiletype.Text.Length == 0) MessageBox.Show("请指定文件后缀名！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                DirectoryInfo Dir = new DirectoryInfo(dir);
                this.progressBar1.Maximum = Dir.GetFiles(txtfiletype.Text).Length;
                this.progressBar1.Minimum = 0;
                int num = 0;
                foreach (FileInfo accounts in Dir.GetFiles(txtfiletype.Text)) //查找文件  
                {
                    num++;
                    accounts.Delete();
                    WriteLog(ImgConst.INFO, " 成功删除：" + accounts.Name);
                    this.progressBar1.Value = num;
                }
                DirectoryInfo[] dirArr = Dir.GetDirectories();
                this.progressBar1.Maximum = this.progressBar1.Maximum + dirArr.Length;
                for (int i = 0; i < dirArr.Length; i++)
                {
                    DirectoryInfo dirInfo = dirArr[i];
                    foreach (FileInfo accounts in dirInfo.GetFiles(txtfiletype.Text)) //查找文件  
                    {
                        num++;
                        accounts.Delete();
                        WriteLog(ImgConst.SUCC, " 成功删除： " + dirInfo.Name + "\\" + accounts.Name);
                        this.progressBar1.Value = num;
                    }

                    DirectoryInfo[] d = dirInfo.GetDirectories();
                    for (int j = 0; j < d.Length; j++)
                    {
                        DirectoryInfo det = d[j];
                        foreach (FileInfo act in det.GetFiles(txtfiletype.Text))
                        {
                            num++;
                            act.Delete();
                            WriteLog(ImgConst.SUCC, " 成功删除： " + dirInfo.Name + "\\" + det.Name + "\\" + act.Name);
                        }
                    }
                }
                this.progressBar1.Value = 0;
                WriteLog(ImgConst.INFO, " 删除完成！");
            }
            catch (Exception ex)
            {
                WriteLog(ImgConst.WARN, ex.Message);
            }
            #endregion
        }
    }

    public class ImgConst
    {
        public const string INFO = "info";
        public const string SUCC = "succ";
        public const string WARN = "warn";

        public ImgConst()
        {
        }
    }
}
